﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HRMSystemShi._2025;
using HRMSystemShi.DAL;

namespace HRMsystemShi._2025
{
    public partial class FormDepart : Form
    {
        public FormDepart()
        {
            InitializeComponent();
        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxDepart.Text))
            {
                CommonHelper.MessageError("部门名称不能为空");
            }
            if (DepartmentService.IsDepartmentExists(textBoxDepart.Text))
            {
                CommonHelper.MessageError("部门已存在");
            }
            else if (DepartmentService.AddDepartment(textBoxDepart.Text))
            {
                CommonHelper.MessageSuccess("添加成功!");
            }
            else
            {
                CommonHelper.MessageError("添加失败");
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            dataGridViewDepart.DataSource = DepartmentService.GetDepartMentDataTable();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewDepart.SelectedRows.Count == 0)
            {
                CommonHelper.MessageError("请先选择要删除的部门");
            }
            else
            {
                Guid id = (Guid)dataGridViewDepart.SelectedRows[0].Cells["ID"].Value;
                if (CommonHelper.MessageYesOrNo("是否删除该部门?") == DialogResult.Yes)
                {
                    if (DepartmentService.DeleteDepartment(id))
                    {
                        CommonHelper.MessageSuccess("删除成功");
                        buttonSearch_Click(null!, null!);
                    }
                    else
                    {
                        CommonHelper.MessageError("删除失败");
                    }
                }
            }
        }
    }
}
